import time
import re
import collections
from dateutil.parser import parse

log_line_1 = '''"60.29.197.77" "-" "[01/Mar/2019:13:58:34 +0800]" "POST /center_h/user/login HTTP/1.1" "200" "130" "371" "http://gxh.izhixue.org/login.html?appid=1&redirect_uri=http://47.92.91.190/oauthlogin/oauthlogin.ashx?scope=web_login&response_type=code" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)" "-" "gxh.izhixue.org" "0.069" "0.064" "127.0.0.1:8080" "200" "myuserid=D79DCD3CCFC7785C6D27F52702040303"'''

log_line_2 = '''"123.151.24.40" "-" "[01/Mar/2019:13:49:34 +0800]" "GET /center_h/order/teacher?pageNo=4&pageSize=15&schoolId=0&time=1551419404251 HTTP/1.1" "200" "4523" "4724" "http://gxh.izhixue.org/order.html" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "-" "gxh.izhixue.org" "0.084" "0.084" "127.0.0.1:8080" "200" "myuserid=D79DCD3CD5C7785C70279642020C0303"'''

pattern_01 = r'''"(?P<remote_addr>[^"]+)" "(?P<remote_user>[^"]+)" "(?P<time_local>[^"]+)" "(?P<request>[^"]+)" "(?P<status>[^"]+)" "(?P<body_bytes_sent>[^"]+)" "(?P<bytes_sent>[^"]+)" "(?P<http_referer>[^"]+)" "(?P<http_user_agent>[^"]+)" "(?P<http_x_forwarded_for>[^"]+)" "(?P<http_host>[^"]+)" "(?P<request_time>[^"]+)" "(?P<upstream_response_time>[^"]+)" "(?P<upstream_addr>[^"]+)" "(?P<upstream_status>[^"]+)" "(?P<myuserid>[^"]+)"'''

pattern_02 = r'''"(?P<remote_addr>[^"]+)" "(?P<remote_user>[^"]+)" "\[(?P<day>\d+)/(?P<month>\w+)/(?P<year>\d+):(?P<time>\S+) (?P<time_zone>\S+)\]" "(?P<request>[^"]+)" "(?P<status>[^"]+)" "(?P<body_bytes_sent>[^"]+)" "(?P<bytes_sent>[^"]+)" "(?P<http_referer>[^"]+)" "(?P<http_user_agent>[^"]+)" "(?P<http_x_forwarded_for>[^"]+)" "(?P<http_host>[^"]+)" "(?P<request_time>[^"]+)" "(?P<upstream_response_time>[^"]+)" "(?P<upstream_addr>[^"]+)" "(?P<upstream_status>[^"]+)" "(?P<myuserid>[^"]+)"'''

# time_local_pattern = r'''(?P<day>\d+)/(?P<month>\w+)/(?P<year>\d+):(?P<time>\S+) (?P<time_zone>\S+)'''
time_local_pattern = r'''\[(?P<day>\d+)/(?P<month>\w+)/(?P<year>\d+):(?P<time>\S+) (?P<time_zone>\S+)\]'''

pattern_03 = r'''(?P<method>\w+) (?P<URI>\S+) (?P<http_protocol>\S+)'''


def mytest1():
    regex = re.compile(pattern_02)
    mo = regex.search(log_line_2)
    print(mo.group('request'))  # POST /center_h/user/login HTTP/1.1
    request_str = mo.group('request')
    regex1 = re.compile(pattern_03)
    mo1 = regex1.search(request_str)
    print(mo1.groups())
    if '?' in mo1.group('URI'):
        print("True")
        str1_list = mo1.group('URI').split('?', 1)
        print(str1_list)
        print(str1_list[0])
    else:
        print("False")


def mytest():
    regex = re.compile(pattern_01)
    mo = regex.search(log_line)
    # print(mo.group('time_local')) # [01/Mar/2019:13:58:34 +0800]
    time_local = mo.group('time_local')

    regex1 = re.compile(time_local_pattern)
    mo1 = regex1.search(time_local)
    print(mo1.groups())
    time_local_str = '%s-%s-%s %s %s' % (
        mo1.group('year'), mo1.group('month'), mo1.group('day'), mo1.group('time'), mo1.group('time_zone'))
    # print(time_local_str)#2019-Mar-01 13:58:34 +0800
    d = parse(time_local_str)
    # print(type(d))  # <class 'datetime.datetime'>
    # print(d)#2019-03-01 13:58:34+08:00


def main():
    regex = re.compile(pattern_02)
    mo = regex.search(log_line)
    # print(mo.groups())   # [01/Mar/2019:13:58:34 +0800]

    # print(mo1.groups()) #('01', 'Mar', '2019', '13:58:34')
    print(mo.groups())


if __name__ == '__main__':
    mytest1()
